package com.evenmo.poi;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * This is Description 读取Excel
 *
 * @author moyongjun
 * @date 2019/12/02
 */
public class ReadExcelDemo {
    /**
    * @author: moyongjun
    * @Date: 2019/12/2 11:53 上午
    * @Description: 读取excel
    *
    */
    public void readExcel(){

        //加载文件
        String fileName="OutputExcelDemo.xls";
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fileName);
        } catch (FileNotFoundException e) {
            System.out.println("请检查文件路径！");
            e.printStackTrace();
        }

        //创建工作薄
        HSSFWorkbook workbook= null;
        try {
            workbook = new HSSFWorkbook(fis);
        } catch (IOException e) {
            System.out.println("创建工作薄失败！");
            e.printStackTrace();
        }
        //创建工作表
        HSSFSheet sheet = workbook.getSheetAt(0);
        //创建行
        HSSFRow row = sheet.getRow(2);
        //创建单元格
        HSSFCell cell = row.getCell(2);

        String stringCellValue = cell.getStringCellValue();

        System.out.println("第三行第三列的值为："+stringCellValue);

        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
